package cn.virens.dd.simple.value;

import java.util.Date;

import cn.hutool.core.convert.Convert;
import cn.virens.dd.StrategyValue;
import cn.virens.dd.utils.SunTimesUtil;

public class StrategyValueByLatLng implements StrategyValue {
	private final Date sunrise;
	private final Date sunset;

	public StrategyValueByLatLng(StrategyValueByLatLng other) {
		this.sunrise = new Date(other.sunrise.getTime());
		this.sunset = new Date(other.sunset.getTime());
	}

	public StrategyValueByLatLng(Object longitude, Object latitude) {
		this(toDouble(longitude), toDouble(latitude), new Date());
	}

	public StrategyValueByLatLng(Double longitude, Double latitude, Date time) {
		this.sunrise = getSunrise(longitude, latitude, time);
		this.sunset = getSunset(longitude, latitude, time);
	}

	public Date getSunset() {
		return sunset;
	}

	public Date getSunrise() {
		return sunrise;
	}

	private static Double toDouble(Object obj) {
		return Convert.toDouble(obj, 0D);
	}

	/** 根据经纬度计算指定日期的日落时间 */
	private static Date getSunset(Double longitude, Double latitude, Date time) {
		return SunTimesUtil.getSunset(longitude, latitude, time);
	}

	/** 根据经纬度计算指定日期的日出时间 */
	private static Date getSunrise(Double longitude, Double latitude, Date time) {
		return SunTimesUtil.getSunrise(longitude, latitude, time);
	}
}
